﻿using SerbinAV.MobileSales.Sales.Common;
using SerbinAV.MobileSales.Sales.Views;
using System;
using SerbinAV.MobileSales.Sales.Entities;
namespace SerbinAV.MobileSales.Sales.Presenters
{
    class DailyVisitsPresenter:IPresenter
    {
        private IWork _work;
        private IDailyVisitsView _view;
        private DateTime _workDate;
        

        public DailyVisitsPresenter(IWork work, IDailyVisitsView view, DateTime dateTime)
        {
            _work = work;
            _view = view;
            _workDate = dateTime;
            //        _view.AddVisit += AddVisitDialog;
             //       _view.DeleteVisit += DeleteDailyVisit;
                    _view.CreateFromShedule += CreateVisitsFromShedule;

        //    try
        //    {
        //        _dbVisit = DataManager.GetInstance().Visists;
        //        _visitDate = dateTime;
        //        _view = view;
        //        _view.DataSource = _dbVisit.GetVisitsByDate(_visitDate,false);


        //    }
        //    catch (Exception ex)
        //    {
        //        ExceptionWork.ExeptionProcess(ex);
        //    }

        }

        public void CreateVisitsFromShedule(object sender, EventArgs e)
        {
            try
            {
                _work.DataManager.Visits.CreateVisitFromGraphics(_workDate);
                _view.DataSource = _work.DataManager.Visits.GetVisitsByDate(_workDate);
            }
            catch (Exception ex)
            {
                _work.HandlerException.Go("Неможливо створити візити", "Помилка при створенні візитів",ex);
            }
        }

        //public void CancelAddVisitDialog(object sender, EventArgs e)
        //{
        //    _view.CloseAddVisitDialog();
        //}

        public void AddVisit(object sender, EventArgs e)
        {

            try
            {
                //AddVisitDialog addVisitDialog = (AddVisitDialog)sender;
                Visit visit = new Visit();
                visit.DateVisit = _workDate;
                //visit.BuyPoint = ((BuyPoint)addVisitDialog.SelectedBuyPoint);
                //_dbVisit.CreateVisit(visit);
                //_view.DataSource = _dbVisit.GetVisitsByDate(_visitDate, false);
            }
            catch (Exception ex)
            {
                //ExceptionWork.ExeptionProcess(ex);
            }
        }

        //public void AddVisitDialog(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        AddVisitDialog addVisitDialog = new AddVisitDialog();
        //        addVisitDialog.OK += AddVisit;
        //        addVisitDialog.Cancel += CancelAddVisitDialog;
        //        _view.ShowAddVisitDialog(addVisitDialog);
        //    }
        //    catch (Exception ex)
        //    {
        //        ExceptionWork.ExeptionProcess(ex);
        //    }
        //}

        public void DeleteDailyVisit(object sender, EventArgs e)
        {
            try
            {
                Visit currentVisit = (Visit)_view.Current;
                _work.DataManager.Visits.DeleteVisit(currentVisit);
                _view.DataSource = _work.DataManager.Visits.GetVisitsByDate(_workDate);
            }

            catch (Exception ex)
            {
                _work.HandlerException.Go(ex.Message, "Помилка при видалені візиту", ex);
            }
        }
        public bool Go()
        {
            _view.DataSource = _work.DataManager.Visits.GetVisitsByDate(_workDate);
            _view.Show();
            return true;
        }
    }
}
